package com.zhiqianbao.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.activity.TakeAccesssoryPicActivity;
import com.zhiqianbao.entity.UserAttachment;
import com.zhiqianbao.utils.Constants;

import java.util.List;

/**
 * Created by liang on 2016/1/5.
 */
public class UserAttachmentListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<UserAttachment> list;
    private Activity ctx;

    public UserAttachmentListAdapter(Activity context, List<UserAttachment> list) {
        this.ctx = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    private final class ListItemView {
        public RelativeLayout relativeLayoutMineHouseCapital;
        public TextView textViewTitle;
        public TextView textViewStatus;
        public ImageView viewUserAttachment;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_attachment, null);
            listItemView = new ListItemView();
            listItemView.relativeLayoutMineHouseCapital = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_mine_house_capital);
            listItemView.textViewTitle = (TextView) convertView.findViewById(R.id.textView_title);
            listItemView.textViewStatus = (TextView) convertView.findViewById(R.id.TextView_status);
            listItemView.viewUserAttachment = (ImageView) convertView.findViewById(R.id.view_user_attachment);
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        String title = list.get(position).getTitle();
        String status = list.get(position).getState();

        listItemView.textViewTitle.setText(title);
        if (Constants.USER_AUDIT_STATE_FAILED.equals(status)) {
            listItemView.textViewStatus.setText("未通过");
        } else if (Constants.USER_AUDIT_STATE_SUCCESS.equals(status)) {
            listItemView.textViewStatus.setText("已通过");
        } else {
            listItemView.textViewStatus.setText("未审核");
        }

        listItemView.relativeLayoutMineHouseCapital.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserAttachment userAttachment = list.get(position);
                Intent intent = new Intent(ctx, TakeAccesssoryPicActivity.class);
                intent.putExtra("userAttachment", userAttachment);
                intent.putExtra("userAttachmentId", userAttachment.getId());
                ctx.startActivityForResult(intent, 1);
            }
        });
        return  convertView;
    }
}
